/**
 * 
 */
package fr.isima.isipaint.model;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;

import javax.swing.SwingUtilities;

/**
 * @author daniel
 * 
 */
public final class Rectangle extends GraphicObject {

	/**
	 * Default constructor
	 */
	public Rectangle() {
		super();
	}

	/**
	 * 
	 * @param x
	 *            The x coordinate of the rectangle
	 * @param y
	 *            The y coordinate of the rectangle
	 * @param width
	 *            The width of the rectangle
	 * @param height
	 *            The height of the rectangle
	 */
	public Rectangle(int x, int y, int width, int height) {
		super(x, y, width, height);
		setVisible(true);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
	 */
	@Override
	protected void paintComponent(Graphics g) {

		Point p = SwingUtilities.convertPoint(getParent(), new Point(getX(), getY()), this);
		Graphics2D g2 = (Graphics2D) g;
		super.paintComponent(g2);		

		g2.setColor(color);
		if (filled) {
			//System.out.println("Filled Rect");
			g2.fillRect(p.x, p.y, getWidth(),
					getHeight());
		} else {
			g2.drawRect(p.x, p.y,
					(int) (getWidth() - stroke.getLineWidth()),
					(int) (getHeight() - stroke.getLineWidth()));
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.JComponent#contains(int, int)
	 */
	@Override
	public boolean contains(int x, int y) {
		boolean res = (x >= getX() && x <= getX() + getWidth() && y >= getY() && y <= getY()
				+ getHeight());
		return res;
	}

}
